import QtQuick 2.15
import QtQuick.Controls 2.15
import '../../controls'
//库存查询--售货
Rectangle {
    anchors.fill: parent
    gradient: mainGradient
    property int pageNo: 0
    property int pageSize: 50
    property bool loading: false
    property bool finished: false
    Component.onCompleted: {
    }

    function loadData()
    {
        if(pageNo <= 1)
        {
            lvMode.clear()
        }

        loading=true
        var request = HttpHelper.createIMRequest();
//        request.enabledLog=true
        request.onSucceed( function(request){
//            console.log('----water_get_goods_list---',JSON.stringify(request.responseData))
            finished=request.responseData.list.length === 0
            for(let i=0;i<request.responseData.list.length;i++)
            {
                let rdata=request.responseData.list[i]
                lvMode.append({ddata:rdata})
            }
            loading=false
        } );
        request.onError(  function(request){
            loading=false
            CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_get_goods_list?page="+pageNo+'&limit='+pageSize+'&code='+textKey.text)
    }
    ListModel {
         id:lvMode
     }

    Item {
        id:itemTop
        width: parent.width
        height: 80
        Row{
            spacing: 10
            anchors.centerIn: parent
            Text {
                color: '#ffffff'
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("关键字")
            }
            Rectangle{
                width: 500
                height: 43
                color: '#ffffff'
                radius:5
                TextField{
                    id:textKey
                    x:50
                    height: parent.height-2
                    width: parent.width - x - 4
                    font.pixelSize: 14
                    maximumLength:30
                    color: '#333333'
                    placeholderText: qsTr('输入查询内容')
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    background: Item{}
                }
            }

            Item {
                width: 50
                height: 1
            }
            Button{
                width: 120
                height: 40
                background: Rectangle{
                    radius: 10
                    color: '#EA9508'
                    Text {
                        anchors.centerIn: parent
                        color: '#ffffff'
                        text: qsTr("查询")
                    }
                }
                onClicked: {
                    pageNo=1
                    loadData()
                }
            }
        }
    }
    Rectangle {
        border.color: '#dddddd'
        anchors.fill: parent
        anchors.topMargin: 100
        anchors.leftMargin: 20
        anchors.bottomMargin: 20
        anchors.rightMargin: 20
        Rectangle {
            id:itemHeader
            width: parent.width - 2
            anchors.horizontalCenter: parent.horizontalCenter
            height: 40
            color: '#F5F7FA'
            property var itemWidth: width / 7
            Row{
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height
                width: parent.width
                Repeater{
                    model: [qsTr('编号'),qsTr('商品图片'),qsTr('商品名称'),qsTr('商品编码'),qsTr('厂家'),qsTr('库存量'),qsTr('单价')/*,qsTr('操作')*/]
                    Text {
                        width: itemHeader.itemWidth
                        height: itemHeader.height
                        color: '#E0000000'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: modelData
                        elide: Text.ElideRight
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }
            }
        }
        ListView{
            id:lvs
            anchors.fill: parent
            anchors.topMargin: itemHeader.height
            model: lvMode
            ScrollBar.vertical: ScrollBar{}
            clip: true
            property var itemWidth: width / 7
            property var itemHeight: 70
            boundsBehavior: Flickable.StopAtBounds
            delegate: Rectangle{
                width: lvs.width
                height: lvs.itemHeight
                color: index % 2 === 0 ? '#FFF2F2F2' : '#ffffff'
                Row{
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.id
                        elide: Text.ElideRight
                    }
                    Item {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        Image {
                            sourceSize: Qt.size(lvs.itemHeight - 10, lvs.itemHeight - 10)
                            asynchronous: true
                            smooth: true
                            source: ddata.img
                            anchors.centerIn: parent
                        }
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.name
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.code
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.intro
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text: ddata.num
                        elide: Text.ElideRight
                    }
                    Text {
                        width: lvs.itemWidth
                        height: lvs.itemHeight
                        color: '#333333'
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        text:  ddata.price.toFixed(2)
                        elide: Text.ElideRight
                        wrapMode: Text.WrapAnywhere
                    }
//                    Button {
//                        width: lvs.itemWidth
//                        height: lvs.itemHeight
//                        background: Item {
//                            Text {
//                                color: 'blue'
//                                text: '详情'
//                                anchors.centerIn: parent
//                            }
//                        }
//                    }

                }
            }
            onAtYEndChanged: {
                if(atYEnd && !loading && !finished)
                {
                    pageNo++
                    console.log('-------load page-----------',pageNo)

                    loadData()
                }
            }
        }
    }
}
